package com.formula.propertyshop.view.phone;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.formula.propertyshop.R;
import com.formula.propertyshop.adapter.PropertyLocationSpinnerAdapter;
import com.formula.propertyshop.adapter.PropertyTypeSpinnerAdapter;
import com.formula.propertyshop.model.PropertyLocation;
import com.formula.propertyshop.model.PropertyType;
import com.formula.propertyshop.service.IWireFrameService;
import com.formula.propertyshop.service.WireFrameServiceFactory;
import com.formula.propertyshop.utils.Const;
import com.formula.propertyshop.utils.WFErrorCode;
import com.formula.propertyshop.utils.WFUtils;
import com.formula.propertyshop.view.common.BaseActivity;

public class SearchActivity extends BaseActivity{

	private RadioButton rdAll;
	private RadioButton rdSale;
	private RadioButton rdRent;
	private Spinner cbbType;
	private Spinner cbbLocation;
	private Spinner cbbBeds;
	private Spinner cbbBaths;
	private EditText txtPriceMax;
	private EditText txtPriceMin;
	private Button btnSearch;
	private String priceMode="";
	//private GridView gView;
	private List<PropertyType> pTypes;
	private List<PropertyLocation>pLocation;
	//private PropertyGirdViewAdapter adapter;
	//private boolean loadingMore = true;
	//private int page=1;
	public void onCreate(Bundle saveInstance){
		super.onCreate(saveInstance);
		if(!WFUtils.checkInternetConnection(this)){
			showNetworkErrorDlg();
			return;
		}
	}
	@Override
	protected void initUI() {
		setContentView(R.layout.search);
		
		rdAll = (RadioButton) findViewById(R.id.rdAll);
		rdRent = (RadioButton) findViewById(R.id.rdRent);
		rdSale = (RadioButton) findViewById(R.id.rdSale);
		cbbType = (Spinner) findViewById(R.id.cbbType);
		cbbLocation = (Spinner) findViewById(R.id.cbbLocation);
		cbbBeds = (Spinner) findViewById(R.id.cbbBeds);
		cbbBaths = (Spinner) findViewById(R.id.cbbBaths);
		txtPriceMax = (EditText) findViewById(R.id.txtPriceMax);
		txtPriceMin = (EditText) findViewById(R.id.txtPriceMin);
		btnSearch = (Button) findViewById(R.id.btnSearchProperty);
		//gView = (GridView) findViewById(R.id.gridPropety);
		TextView title = (TextView) findViewById(R.id.txtTitle);
		title.setText(getString(R.string.search));
		setControls();
		loadData();
	}
	private void setControls(){
		btnSearch.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (rdAll.isChecked()){priceMode=rdAll.getText().toString();}
				else if (rdRent.isChecked()){priceMode=rdRent.getText().toString();}
				else if (rdSale.isChecked()){priceMode=rdSale.getText().toString();}
				
				String beds="";
				String baths="";
				if (cbbBaths.getSelectedItemPosition()!=0){
					beds=cbbBeds.getSelectedItem().toString();
				}
				if (cbbBaths.getSelectedItemPosition()!=0){
					beds=cbbBaths.getSelectedItem().toString();
				}
				String url = "sale-type="+priceMode+"&location="+((PropertyLocation)cbbLocation.getSelectedItem()).getId()+"&type="+((PropertyType)cbbType.getSelectedItem()).getId()+"&beds="+beds+"&baths="+baths+"&min="+txtPriceMin.getText().toString()+"&max="+txtPriceMax.getText().toString()+"&paging=";
				Intent intent = new Intent(SearchActivity.this,SearchResultActivity.class);
				intent.putExtra(Const.PROPERTYID, url);
				startActivity(intent);
			}
		});
		
	}

	private void loadData() {
		showProgressDlg();

		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				IWireFrameService service = WireFrameServiceFactory.getInstance().getService();
				Message msg = handler.obtainMessage();
				try{
					pTypes = service.getPropertyTypes();
					PropertyType type = new PropertyType();
					type.setId("");
					type.setValue(getString(R.string.search_type));
					pTypes.add(0,type);
					pLocation = service.getPropertyLocations();
					PropertyLocation location = new PropertyLocation();
					location.setId("");
					location.setName(getString(R.string.search_location));
					pLocation.add(0,location);
				}
				catch(Exception e){
					e.printStackTrace();
					msg.arg1 = WFErrorCode.NETWORK_ERROR.getValue();
				}
				//msg.obj = pTypes;
				handler.sendMessage(msg);
			}
		});
		thread.start();
	}
	
	private Handler handler = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();
			if(pTypes==null||pLocation==null){
				return;
			}
			PropertyTypeSpinnerAdapter adapterType = new PropertyTypeSpinnerAdapter(SearchActivity.this, android.R.layout.simple_spinner_item, pTypes);
			adapterType.setDropDownViewResource(R.layout.dropdown_item);
			cbbType.setAdapter(adapterType);
			PropertyLocationSpinnerAdapter adapterLoc = new PropertyLocationSpinnerAdapter(SearchActivity.this, android.R.layout.simple_spinner_item, pLocation);
			adapterLoc.setDropDownViewResource(R.layout.dropdown_item);
			cbbLocation.setAdapter(adapterLoc);
			ArrayAdapter<CharSequence>bedsAdapter = ArrayAdapter.createFromResource(SearchActivity.this, R.array.beds_array, R.layout.spinner_item);
			bedsAdapter.setDropDownViewResource(R.layout.dropdown_item);
			cbbBeds.setAdapter(bedsAdapter);
			ArrayAdapter<CharSequence>bathsAdapter = ArrayAdapter.createFromResource(SearchActivity.this, R.array.baths_array, R.layout.spinner_item);
			bathsAdapter.setDropDownViewResource(R.layout.dropdown_item);
			cbbBaths.setAdapter(bathsAdapter);
		}
	};
	
}
